<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_maxred'=>1,'is_agent'=>1);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),		
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		$maxlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','level desc');
		foreach($list as &$item){
			if($item['level_id']!=$maxlevel['id']){
				$this->update('member',array('is_maxred'=>0),array('id'=>$item['id']));
			}
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent']=cfc('core')->member_show($item['fromuser']);
			$item['url']=custom_url('jy_merch2fee/myhome/main/mid='.$item['mid'].'&fromuser='.$item['mid'],true);
			$item['appurl']='jy_merch2fee/myhome//mid='.$item['mid'].'&fromuser='.$item['mid'];
			$item['commission_1']=$this->sum('commission',array('status <='=>1,'mid'=>$item['mid']),'money');
			$item['commission_2']=$this->sum('commission',array('status'=>2,'mid'=>$item['mid']),'money');
			$item['isdongfee']=$this->sum('commission',array('isjiedong'=>0,'mid'=>$item['mid']),'dong_fee');
			$item['order_num']=$this->count('order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']));
			$item['order_price']=pdo_getsum('jy_merch2fee_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']),'order_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：</br>1、所有股东的分红，可以等级里面设置或独立商品里面设置！','edit'=>false,'delone'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'agent','title'=>'上级信息','type'=>'member'),
			array('name'=>'url','title'=>'推广二维码','type'=>'ewm','action'=>'appurl'),
			array('name'=>'level_id','title'=>'股东等级','type'=>'select','options'=>'levels'),
			array('name'=>'commission_1','title'=>'待结算','type'=>'string'),
			array('name'=>'commission_2','title'=>'已结算','type'=>'string'),
			array('name'=>'isdongfee','title'=>'冻结中','type'=>'string'),
			array('name'=>'order_num','title'=>'团订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'团订单总金额','type'=>'string'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/max_redboss/updata/','title'=>'一键刷新老板'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'预选股东');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'is_maxred'=>1);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$order=$this->pdo_getall('member',$params);
	$agent_status=$this->agent_status;
	if($order){
		foreach($order as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['order_num']=$this->count('order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']));
			$item['order_price']=pdo_getsum('jy_merch2fee_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['mid']),'order_price');
			$item['level_title']=$this->pdo_getcolumn('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['level_id']),'title');
			$item['level_title']=$item['level_title']?:'预选股东';
			$item['statustitle']=$agent_status[$item['status']];
			$item['nickname']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$item['address']=$item['address']?:'无';
			$item['order_price']=$item['order_price']?:'0.00';
		}
	}
	$header=array(
		'nickname'=>'团队昵称',
		'mobile'=>'团队电话',
		'level_title'=>'股东等级',
		'statustitle'=>'股东状态',
		'order_num'=>'团订单数',
		'order_price'=>'团订单总金额',
	);
	cfc('tools')->export2excel($header,$order,'股东信息');
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	$item=$this->get('member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['member']=model('core/member')->detail($item['mid'],array('nickname','avatar','realname','mobile','wechat'));
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['realname']?:$item['member']['wechat'];
	$item['diyformfields']=$item['diyformdata'];
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'股东信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformfields',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='updata'){
	$maxlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','level desc');
	$members=$this->getall('member',array('level_id'=>$maxlevel['id'],'is_maxred'=>0,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']));
	foreach($members as $item){
			$xiaji=$this->count('member',array('level_id'=>$maxlevel['id'],'fromuser'=>$item['mid'],'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','level_id'));
			if($xiaji){
				$this->update('member',array('is_maxred'=>1),array('id'=>$item['id']));
			}else{
				$this->update('member',array('is_maxred'=>0),array('id'=>$item['id']));
			}
	}	
	exi('一键更新成功！');
}
?>